package com.ruoyi.report_lqhhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lqhhl.domain.bo.ReportLqhhlLqhlkljpjlbBsDetailBo;
import com.ruoyi.report_lqhhl.domain.vo.ReportLqhhlLqhlkljpjlbBsDetailVo;
import com.ruoyi.report_lqhhl.domain.ReportLqhhlLqhlkljpjlbBsDetail;
import com.ruoyi.report_lqhhl.mapper.ReportLqhhlLqhlkljpjlbBsDetailMapper;
import com.ruoyi.report_lqhhl.service.IReportLqhhlLqhlkljpjlbBsDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青混合料沥青含量、矿料级配试验检测记录详情Service业务层处理
 *
 * @author llb
 * @date 2024-08-31
 */
@RequiredArgsConstructor
@Service
public class ReportLqhhlLqhlkljpjlbBsDetailServiceImpl implements IReportLqhhlLqhlkljpjlbBsDetailService {

    private final ReportLqhhlLqhlkljpjlbBsDetailMapper baseMapper;

    /**
     * 查询沥青混合料沥青含量、矿料级配试验检测记录详情
     */
    @Override
    public ReportLqhhlLqhlkljpjlbBsDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询沥青混合料沥青含量、矿料级配试验检测记录详情列表
     */
    @Override
    public TableDataInfo<ReportLqhhlLqhlkljpjlbBsDetailVo> queryPageList(ReportLqhhlLqhlkljpjlbBsDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqhhlLqhlkljpjlbBsDetail> lqw = buildQueryWrapper(bo);
        Page<ReportLqhhlLqhlkljpjlbBsDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青混合料沥青含量、矿料级配试验检测记录详情列表
     */
    @Override
    public List<ReportLqhhlLqhlkljpjlbBsDetailVo> queryList(ReportLqhhlLqhlkljpjlbBsDetailBo bo) {
        LambdaQueryWrapper<ReportLqhhlLqhlkljpjlbBsDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqhhlLqhlkljpjlbBsDetail> buildQueryWrapper(ReportLqhhlLqhlkljpjlbBsDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqhhlLqhlkljpjlbBsDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportLqhhlLqhlkljpjlbBsDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getShaikcc()), ReportLqhhlLqhlkljpjlbBsDetail::getShaikcc, bo.getShaikcc());
        lqw.eq(bo.getShaisz1() != null, ReportLqhhlLqhlkljpjlbBsDetail::getShaisz1, bo.getShaisz1());
        lqw.eq(bo.getFenjsy1() != null, ReportLqhhlLqhlkljpjlbBsDetail::getFenjsy1, bo.getFenjsy1());
        lqw.eq(bo.getGonggbfl1() != null, ReportLqhhlLqhlkljpjlbBsDetail::getGonggbfl1, bo.getGonggbfl1());
        lqw.eq(bo.getShaisz2() != null, ReportLqhhlLqhlkljpjlbBsDetail::getShaisz2, bo.getShaisz2());
        lqw.eq(bo.getFenjsy2() != null, ReportLqhhlLqhlkljpjlbBsDetail::getFenjsy2, bo.getFenjsy2());
        lqw.eq(bo.getGonggbfl2() != null, ReportLqhhlLqhlkljpjlbBsDetail::getGonggbfl2, bo.getGonggbfl2());
        lqw.eq(bo.getXiuzxs() != null, ReportLqhhlLqhlkljpjlbBsDetail::getXiuzxs, bo.getXiuzxs());
        lqw.eq(bo.getTonggl() != null, ReportLqhhlLqhlkljpjlbBsDetail::getTonggl, bo.getTonggl());
        lqw.eq(bo.getXiuzhtgl() != null, ReportLqhhlLqhlkljpjlbBsDetail::getXiuzhtgl, bo.getXiuzhtgl());
        lqw.eq(bo.getZuidz() != null, ReportLqhhlLqhlkljpjlbBsDetail::getZuidz, bo.getZuidz());
        lqw.eq(bo.getZuixz() != null, ReportLqhhlLqhlkljpjlbBsDetail::getZuixz, bo.getZuixz());
        return lqw;
    }

    /**
     * 新增沥青混合料沥青含量、矿料级配试验检测记录详情
     */
    @Override
    public Boolean insertByBo(ReportLqhhlLqhlkljpjlbBsDetailBo bo) {
        ReportLqhhlLqhlkljpjlbBsDetail add = BeanUtil.toBean(bo, ReportLqhhlLqhlkljpjlbBsDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青混合料沥青含量、矿料级配试验检测记录详情
     */
    @Override
    public Boolean updateByBo(ReportLqhhlLqhlkljpjlbBsDetailBo bo) {
        ReportLqhhlLqhlkljpjlbBsDetail update = BeanUtil.toBean(bo, ReportLqhhlLqhlkljpjlbBsDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqhhlLqhlkljpjlbBsDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青混合料沥青含量、矿料级配试验检测记录详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
